package com.zpeb.pdd.service.impl;

import com.zpeb.pdd.entity.User;
import com.zpeb.pdd.mapper.UserMapper;
import com.zpeb.pdd.service.UserInfo;
import com.zpeb.pdd.utils.CommonUtils;
import com.zpeb.pdd.utils.MD5;
import com.zpeb.pdd.utils.Msg;
import org.omg.PortableInterceptor.USER_EXCEPTION;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName pdd
 * @Description: TODO
 * @date ${DAT}10:33
 */

@Service
public class Userimpl implements UserInfo {

    @Autowired
    UserMapper userMapper;

    @Override
    public int addUser(User user) throws Exception {
        if (user.getPassword() == null
                || user.getPassword().equals("")
                || user.getUsername() == null
                || user.getUsername().equals("")){
            return 0;
        }
        user.setUserid(CommonUtils.getUUID());
        user.setPassword(MD5.md5Encode(user.getPassword()));
        return userMapper.insertSelective(user);
    }

    @Override
    public int updateUser(User user,String id) throws Exception {
        User isExist = userMapper.selectByPrimaryKey(id);
        if (isExist == null) {
            return 0;
        } else {
            if (user.getPassword() != null && !user.getPassword().equals("")) {
                user.setPassword(MD5.md5Encode(user.getPassword()));
            }
            user.setUserid(id);
            return userMapper.updateByPrimaryKeySelective(user);
        }
    }

    @Override
    public User selectById(String id) throws Exception {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<User> selectAll() throws Exception {
        return userMapper.selectAll();
    }

    @Override
    public int deleteUser(String id) throws Exception {
        return userMapper.deleteByPrimaryKey(id);
    }

    /**
     * 登录使用
     * @param userName
     * @return
     * @throws Exception
     */
    @Override
    public User getPasswordByUsername(String userName) throws Exception {
        return userMapper.getPasswordByUsername(userName);
    }

    @Override
    public List<User> getRoleBUser() throws Exception {
        return userMapper.getRoleBUser();
    }

    @Override
    public List<User> getRoleCUser() throws Exception {
        return userMapper.getRoleCUser();
    }
}
